package com.xjh.him.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xjh.him.dao.FriendGroupMsgMapper;
import com.xjh.him.domain.FriendGroupMsgDO;
import com.xjh.him.dto.friendgroupmsg.FriendGroupMsgDTO;
import com.xjh.him.dto.friendgroupmsg.FriendGroupMsgQueryDTO;
import com.xjh.him.netty.HandlerListener;
import com.xjh.him.netty.NettyCore;
import com.xjh.him.netty.codec.Command;
import com.xjh.him.netty.codec.Packet;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xjh.tool.Checks;
import org.xjh.tool.common.ErrorMsg;
import org.xjh.tool.dto.ResultDTO;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class FriendGroupMsgServiceImpl
        implements IFriendGroupMsgService, HandlerListener {

    @Autowired
    private FriendGroupMsgMapper friendGroupMsgMapper;

    @Override
    public ResultDTO<Integer> countHistoryMsg(Long groupId) {
        if (Checks.isEmpty(groupId)) {
            return ResultDTO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        QueryWrapper<FriendGroupMsgDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("friend_group_id", groupId);
        Integer count = friendGroupMsgMapper.selectCount(queryWrapper);
        return ResultDTO.getSuccessResult(count);
    }

    @Override
    public ResultDTO<List<FriendGroupMsgDTO>> queryHistoryMsg(FriendGroupMsgQueryDTO queryDTO) {
        if (Checks.isAnyEmpty(queryDTO, queryDTO.getGroupId())) {
            return ResultDTO.getFailureResult(ErrorMsg.ERR_PARAMETER_IS_NULL);
        }
        List<FriendGroupMsgDO> msgDOList = friendGroupMsgMapper.queryByGroupId(
                queryDTO.getGroupId(), queryDTO.getSkip(), queryDTO.getLimit());
        List<FriendGroupMsgDTO> dtoList = msgDOList.stream().map(item -> {
            FriendGroupMsgDTO dto = new FriendGroupMsgDTO();
            BeanUtils.copyProperties(item, dto);
            return dto;
        }).collect(Collectors.toList());
        return ResultDTO.getSuccessResult(dtoList);
    }

    private void insertMsg(FriendGroupMsgDTO msgDTO) {
        FriendGroupMsgDO msgDO = new FriendGroupMsgDO();
        BeanUtils.copyProperties(msgDTO, msgDO);
        int insert = friendGroupMsgMapper.insert(msgDO);
        if (insert == 0) {
            log.error("insertMsg:{} failed", msgDO);
        }
    }

    @Override
    public Boolean consume(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof FriendGroupMsgDTO) {
            FriendGroupMsgDTO msgDTO = (FriendGroupMsgDTO) msg;
            //消息写入数据库
            insertMsg(msgDTO);
            //发送消息到接收者
            Channel channel = NettyCore.ACTIVE_USER_CHANNEL.get(msgDTO.getReceiverId());
            if (channel != null) {
                channel.writeAndFlush(Packet.create(Command.FRIEND_GROUP_MSG_SEND, msgDTO));
            }
            return true;
        }

        return false;
    }
}
